<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<script src="./js/jquery-1.7.2.min.js"></script>
</head>
<style>
.fontClass{
	font-size : 20px;
	font-weight : blod;
	letter-spacing : 8px;
}

div.line{
height:5px;
border-top:2px solid black;
width:100%;
}

.shuTestItem{
	margin:auto;
}

</style>
<script>
window. addEventListener ('message', function(event) { 
	//event.data获取传过来的数据
	if(event.data.type == "print"){
		printTest();
	}else if(event.data.type == "test"){
		generate(event.data.testData);
	}
});

function printTest(){
  window.print(); 
}

//判断计算题目是否重复
//只是字符串比较 1+2 和2+1 为false
var maxCacheCount = 60; //多少个以内不重复
var resultCache = []
function isExist(testItem){
	var result = false;
	for(var i = 0;i<resultCache.length;i++){
		if(resultCache[i] == testItem){
			console.log(testItem);
			result = true;
			break;
		}
	}
	
	if(!result){
		resultCache.push(testItem);
		if(resultCache.length>maxCacheCount){
			resultCache.unshift();
		}
	}
	
	return result;
}

//生成计算题目
function generate(options){
	$("#testList").empty();
	resultCache = [];
	
	//console.log(options);
	
	if(options.totalWeight >0){
		var result = [];
		var sumCount = options.total;
		
		var generatCount = 0;
		
		var itemNumberArray=[];
		jQuery.each(options.item,function(i,n){
			itemNumberArray.push(0);
		});
		
		
		while(sumCount >0 ){
			//在总的权重下面,得到一个随机数
			//通过随机数,得到当前需要整哪个计算
			
			var itemWeightArray = [];
			var calTime = 0;
			for(var i = 0 ; i< options.item.length;i++){
				calTime = Math.max(options.item[i].value,calTime);	
			}
			for(var i = 0 ; i<calTime;i++){
				for(var j = 0 ; j< options.item.length;j++){
					if(options.item[j].value>(i)){
						itemWeightArray.push(j);
					}
				}
			}
			
			//console.log(itemWeightArray);
			
			jQuery.each(itemWeightArray,function(i,n){
				var targetScript = options.item[n].method+"();";
				//console.log(targetScript);
				var testItem = eval(targetScript);
				while(!testItem  || (isExist(testItem) && generatCount<options.total*10)){
					testItem = eval(targetScript);
					generatCount++;
				}
				//判断是否重复
				//增加生成次数超过题目总数的十倍,说明选择的项目中,题目一定有重复,跳过isExist的判断,直接生成,否则会死循环
				//if(!isExist(testItem) || generatCount>options.total*10){
					result.push(testItem);
					sumCount --;	
					itemNumberArray[n] = itemNumberArray[n]+1;
				//}
				
			});
		}
		
		//console.log(itemNumberArray);
		
		//开始渲染题目表格
		var table = $("<table></table>").addClass("testTable").attr("width","100%");
		var colCount = 0;
		var lastTr;
		$.each(result,function(i,n){
			colCount = i % options.colNum;
			if(colCount == 0){
				lastTr = $("<tr></tr>").addClass("testTableRow");
				table.append(lastTr);
			}
			
			var col = showTestItem(n);
			var td = $("<td></td>");
			td.append(col);
			lastTr.append(td);
		});
		
		$("#testList").append(table);
	}
}

/**
*	生成单个的计算题目
*/
function showTestItem(testItem){
	
	var testTable = $("<table></table>").addClass("shuTestItem").attr("width","100px");
	var tr = $("<tr></tr>");
	tr.append($("<td></td>"));
	tr.append($("<td></td>").attr("align","right").addClass("fontClass").text(testItem.left));
	testTable.append(tr);
	var tr2 = $("<tr></tr>");
	tr2.append($("<td></td>").attr("align" , "left").addClass("fontClass").text(testItem.flag));
	tr2.append($("<td></td>").attr("align" , "right").addClass("fontClass").text(testItem.right));
	testTable.append(tr2);
	var tr3 = $("<tr></tr>");
	tr3.append($("<td></td>").attr("colspan" , 2).append($("<div></div>").addClass("line")));
	testTable.append(tr3);
	
	if(testItem.total){
		var tr5 = $("<tr></tr>");
		tr5.append($("<td></td>").attr("colspan" , 2).attr("align","right").addClass("fontClass").append($("<div></div>").text(testItem.total)));
		testTable.append(tr5);
	}
	
	var tr4 = $("<tr></tr>").attr("height","50px");
	testTable.append(tr4);
	//$("#listTestList").append(testTable);
	return testTable;
}


//--------------------------------------计算方法

//20以内加减法
function innerTwenty(){
	var flag = Math.round(Math.random() * 1000);
	var one , two , flagStr;
	if(flag % 2 == 0){
		//加法
		flagStr = "✚";
		two = 1+Math.round(Math.random() * 8);
		one = 1+Math.round(Math.random() * 8);
		
	}else{
		flagStr = "一";
		var number1 = 1+Math.round(Math.random() * 8);
		var number2 = 10+Math.round(Math.random() * 9);
		one = number1>number2?number1:number2;
		two = number1<number2?number1:number2;
	}
	return {
	left:one,flag:flagStr,right:two
	}
}
//50以内加减法
function innerFifty(){
	var flag = 1+Math.round(Math.random());
	var one , two , flagStr;
	if(flag>1){
		//加法
		flagStr = "✚";
		two = 10+Math.round(Math.random() * 40);
		one = 10+Math.round(Math.random() * 40);
		
	}else{
		flagStr = "一";
		var number1 = 10+Math.round(Math.random() * 40);
		var number2 = 10+Math.round(Math.random() * 40);
		one = number1>number2?number1:number2;
		two = number1<number2?number1:number2;
	}
	return {
	left:one,flag:flagStr,right:two
	}
}

//50以内加减法填空
function innerFiftyFill(){
	var result = null;
	while(!result){
		result = innerFifty();
	}
	if(result.flag == '✚'){
		result.total = result.left + result.right;
	}else{
		result.total = result.left - result.right;
	}
	return hideNumber2(result);
}

//50以内借位减法
function innerFiftyMinus(){
	var flag = 1+Math.round(Math.random());
	var one , two , flagStr;
	
	flagStr = "一";
	var number1 = 10+Math.round(Math.random() * 40);
	var number2 = 10+Math.round(Math.random() * 40);
	
	one = number1>number2?number1:number2;
	two = number1<number2?number1:number2;
	
	var oneNum = one % 10;
	var twoNum = two % 10;
	if(oneNum < twoNum){
		return {
	left:one,flag:flagStr,right:two
	}
	}
}

//50以内填空借位减法
function innerFillFiftyMinus(){
	var result = null;
	while(!result){
		result = innerFiftyMinus();
	}
	
	result.total = result.left - result.right;
	return hideNumber2(result);
}
/**
隐藏竖式计算的数字
*/
function hideNumber(testItem ){
	var flag = Math.round(100* Math.round(1+Math.random()));
	var type1 = flag % 3;
	var type2 = (type1+1) % 3;
	
	var count = 100*(1+Math.round(Math.random()))% 2 == 1;
	if(type1 == 0){
		testItem.left = changeNumber(testItem.left , count);
	}else if(type1 == 1){
		testItem.right = changeNumber(testItem.right , count);
	}else if(type1 == 2){
		testItem.total = changeNumber(testItem.total , count);
	}
	
	if(type2 == 0){
		testItem.left = changeNumber(testItem.left , !count);
	}else if(type2 == 1){
		testItem.right = changeNumber(testItem.right , !count);
	}else if(type2 == 2){
		testItem.total = changeNumber(testItem.total , !count);
	}
	return testItem;
}

function hideNumber2(testItem ,maxCount){
	var count = maxCount || 2;
	var flag = Math.round(100* Math.round(1+Math.random()));
	
	for(var i = 0 ; i<count ; i++){
		var type1 = flag % 3;
		if(type1 == 0){
			testItem.left = changeNumber2(testItem.left , i+1);
		}else if(type1 == 1){
			testItem.right = changeNumber2(testItem.right , i+1);
		}else if(type1 == 2){
			testItem.total = changeNumber2(testItem.total , i+1);
		}
		flag++;
	}
	
	return testItem;
}
/*
变更数字
number:数字
count:0:十位,1:个位"
*/
function changeNumber(number , count){
	if(count == 0){
		var oneNum = parseInt(number / 10);
		return oneNum + "_";
	}else if(count == 1){
		var oneNum = number % 10;
		return "_"+oneNum;
	}
}

/*
变更数字
number:数字
count:3:百位,2:十位,1:个位"
*/
function changeNumber2(number , count){
	var num = number + "";
	var result = "";
	if(num.length >= count){
		result += num.substr(0 , num.length - count);
	}
	result += "_";
	if(result.length < num.length){
		result += num.substr(result.length);
	}
	return result;
}

//100以内加减法
function innerHundred(){
	var flag = 1+Math.round(Math.random());
	var one , two , flagStr;
	if(flag>1){
		//加法
		flagStr = "✚";
		two = 10+Math.round(Math.random() * 90);
		one = 10+Math.round(Math.random() * 90);
		
	}else{
		flagStr = "一";
		var number1 = 10+Math.round(Math.random() * 90);
		var number2 = 10+Math.round(Math.random() * 90);
		one = number1>number2?number1:number2;
		two = number1<number2?number1:number2;
	}
	return {
	left:one,flag:flagStr,right:two
	}
}

//100以内加减法填空
function innerHundredFill(){
	var result = null;
	while(!result){
		result = innerHundred();
	}
	
	if(result.flag == '✚'){
		result.total = result.left + result.right;
	}else{
		result.total = result.left - result.right;
	}
	return hideNumber2(result , 2);
}

//500以内加减法
function innerFiveHundred(){
	var flag = 1+Math.round(Math.random());
	var one , two , flagStr;
	if(flag>1){
		//加法
		flagStr = "✚";
		two = 10+Math.round(Math.random() * 490);
		one = 10+Math.round(Math.random() * 490);
		
	}else{
		flagStr = "一";
		var number1 = 10+Math.round(Math.random() * 490);
		var number2 = 10+Math.round(Math.random() * 490);
		one = number1>number2?number1:number2;
		two = number1<number2?number1:number2;
	}
	return {
	left:one,flag:flagStr,right:two
	}
}

//500以内加减法填空
function innerFiveHundredFill(){
	var result = null;
	while(!result){
		result = innerFiveHundred();
	}
	
	if(result.flag == '✚'){
		result.total = result.left + result.right;
	}else{
		result.total = result.left - result.right;
	}
	return hideNumber2(result , 3);
}

function inner20Multi(){
	var flagStr = "╳";
	var one , two , flagStr;
	
	two = 10+Math.round(Math.random() * 10);
	one = 10+Math.round(Math.random() * 10);
		
	return {
	left:one,flag:flagStr,right:two
	}
}

function one10Multi(){
	var flagStr = "╳";
	var one , two , flagStr;
	
	two = 1+Math.round(Math.random() * 8);
	one = 9+Math.round(Math.random() * 10);
		
	return {
	left:one,flag:flagStr,right:two
	}
}

</script>
<style>
body{overflow-x：hidden；overflow-y：auto；}

</style>

<body>
<div id="testList"></div>
</body>
</html>